package Data::Conveyor::Exception::Handler;

# $Id: Handler.pm 13653 2007-10-22 09:11:20Z gr $

use warnings;
use strict;
use Data::Miscellany 'class_map';


our $VERSION = '0.02';


use base 'Class::Scaffold::Storable';


use constant ERRCODE_FOR_EXCEPTION_CLASS_HASH => (
    UNIVERSAL => 'NC20000',            # fallback
);


sub RC_FOR_EXCEPTION_CLASS_HASH {
    local $_ = $_[0]->delegate;
    (
        # fallback
        UNIVERSAL                               => $_->RC_INTERNAL_ERROR,

        'Error::Hierarchy::Internal'            => $_->RC_INTERNAL_ERROR,
        'Class::Value::Exception'               => $_->RC_ERROR,
        'Class::Scaffold::Exception::Business' => $_->RC_ERROR,
        'Data::Conveyor::Exception::Ticket'     => $_->RC_INTERNAL_ERROR,
        'Data::Conveyor::Exception::Ticket::MissingLock' => $_->RC_ERROR,
    )
}


sub STATUS_FOR_EXCEPTION_CLASS_HASH {
    local $_ = $_[0]->delegate;
    (
        UNIVERSAL                               => $_->TS_ERROR,    # fallback

        'Error::Hierarchy::Internal'            => $_->TS_ERROR,
        'Class::Value::Exception'               => $_->TS_RUNNING,
        'Class::Scaffold::Exception::Business' => $_->TS_RUNNING,
        'Data::Conveyor::Exception::Ticket'     => $_->TS_ERROR,
        'Data::Conveyor::Exception::Ticket::MissingLock' => $_->TS_RUNNING,
    )
}


sub errcode_for_exception_class {
    my ($self, $class) = @_;
    class_map(
        $class,
        scalar $self->every_hash('ERRCODE_FOR_EXCEPTION_CLASS_HASH')
    );
}


sub rc_for_exception_class {
    my ($self, $exception, $payload_item) = @_;

    # Here we don't use the payload item, but a subclass might use it to
    # change the rc depending on the object type and command found in the
    # payload item.

    $self->delegate->make_obj('value_ticket_rc', 
        class_map(
            $exception,
            scalar $self->every_hash('RC_FOR_EXCEPTION_CLASS_HASH')
        )
    );
}


sub status_for_exception_class {
    my ($self, $exception, $payload_item) = @_;

    # Here we don't use the payload item, but a subclass might use it to
    # change the status depending on the object type and command found in the
    # payload item.

    $self->delegate->make_obj('value_ticket_status', 
        class_map(
            $exception,
            scalar $self->every_hash('STATUS_FOR_EXCEPTION_CLASS_HASH')
        )
    );
}


1;


__END__

{% USE p = PodGenerated %}

=head1 NAME

{% p.package %} - stage-based conveyor-belt-like ticket handling system

=head1 SYNOPSIS

    {% p.package %}->new;

=head1 DESCRIPTION

None yet. This is an early release; fully functional, but undocumented. The
next release will have more documentation.

=head1 METHODS

=over 4

{% p.write_methods %}

=back

{% p.write_inheritance %}

{% PROCESS standard_pod %}

=cut

